package com.zingrow.web.configure.keyword.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.common.utils.AnalyzeResult;
import com.zingrow.web.configure.channel.model.Channel;
import com.zingrow.web.configure.keyword.mapper.KeywordMapper;
import com.zingrow.web.configure.keyword.model.Keyword;
import com.zingrow.web.configure.keyword.service.IKeywordService;

import jxl.Sheet;
import jxl.Workbook;

@Service
public class KeywordService extends BaseService<Keyword> implements IKeywordService{
    
    @Autowired
    private KeywordMapper keywordMapper;

    @Override
    public List<Keyword> queryByParams(PageView pageView, String keywords_seek, Integer keywords_status) {
        Map<String, Object> map = new HashMap<>();
        map.put("paging", pageView);
        map.put("keywords_seek", keywords_seek);
        map.put("keywords_status", keywords_status);
        return keywordMapper.queryByParams(map);
    }

    @Override
    public Keyword queryById(Integer keywords_id) {
        return keywordMapper.selectByPrimaryKey(keywords_id);
    }

    @Override
    public boolean modifyByPrimaryKey(Keyword keyword) {
        if(keyword.getKeywords_status()==null){
            keyword.setKeywords_status(1);
        }
        if(keyword.getKeywords_type()==null || keyword.getKeywords_type().equals("")){
            keyword.setKeywords_type("MetaSearch");
        }
        return keywordMapper.modifyByPrimaryKey(keyword) > 0;
    }

    @Override
    public boolean querySameByMetasearchKeywords(String keywords_seek, Integer keywords_id) {
        return keywordMapper.selectSameByKeywordId(keywords_seek, keywords_id) > 0;
    }

    @Override
    public boolean addMetasearch(Keyword keyword) {
        if(keyword.getKeywords_status()==null || "".equals(keyword.getKeywords_status())){
            keyword.setKeywords_status(1);
        }
        if(keyword.getKeywords_type()==null || keyword.getKeywords_type().equals("")){
            keyword.setKeywords_type("MetaSearch");
        }
        return keywordMapper.add(keyword) > 0;
    }

    @Override
    public boolean deleteMetasearch(Integer keywords_id) {
        return keywordMapper.deleteByPrimaryKey(keywords_id) > 0;
    }

    @Override
    public void initMapper() {
          this.baseMapper=keywordMapper;
    }
    /*导入*/
    @Override
	public void analyzeExcelData(File file,AnalyzeResult<Keyword> result)throws Exception{
   	List<Keyword> list = new ArrayList<Keyword>();
		//解析文件
		Workbook work = Workbook.getWorkbook(file);
		//获取第一个工作表
		Sheet sheet = work.getSheet(0);
		int cols = 0;
		int rows = 0;
		//判断工作表获取是否正常
		if(CommonUtils.isObjectNotNull(sheet)){
			//得到列的数量
			cols = sheet.getColumns();
			//得到行的数量
			rows = sheet.getRows();
			if(cols<=1||rows<=1){
				result.setResult(false, "未检测到数据", new ArrayList<Keyword>());
			}else if(rows>=9999999){
				result.setResult(false, "导入数量过多，请拆分数据分多次导入" + rows, new ArrayList<Keyword>());
			}
		}else{
			//可能是没有sheet,也可能是其他原因
			result.setResult(false, "Excel工作表格格式错误", new ArrayList<Keyword>());
		//判断，数据不合法直接返回
		}if(!result.isSuccess()){
			return;
		}
		//获取正常，进行一下判断
		for (int i = 1; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				String keywords_seek = sheet.getCell(j++, i).getContents().trim();
				String keywords_type = sheet.getCell(j++, i).getContents().trim();
				
				//判断数据合法
				if (keywords_seek.length()<0 || keywords_seek.length()>20) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Keyword>());
				}
				if (StringUtils.isBlank(keywords_seek)) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Keyword>());
				}
							
				if (!StringUtils.isBlank(keywords_type)) {
					if ("百度".equals(keywords_type)||"News".equals(keywords_type)) {
						keywords_type = "百度";					
					} else {
						result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Keyword>());
					}
				} else {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Keyword>());
				}
				// 以上判断通过，添加数据
				if (result.isSuccess()) {
					Keyword keyword = new Keyword(null, keywords_seek,null,keywords_type);
						list.add(keyword);
				} else {
					return;
				}
			}
		}
		result.setResult(true, null, list);
		
	}
    

  	
  	
}
